<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */
	defined ('IN_AGORA') or die;

	class TaskController extends AgoraPostController
	{
		function _default()
		{
			$can_edit = $this->access_model->authenticateTopic($this->agora_user['id'], $this->id,'edit_topic');
			
			$icons_model = & Model::getInstance('TopicIconsModel');
			$this->view->assign('topic_id',intval($this->id));
			$this->view->assignRef('topic_icons',$icons_model->loadAll());

			$this->view->template = 'post/edit_topic';

			$topic_model = & Model::getInstance('TopicModel');
			$topic = $topic_model->load($this->id);

			$catnav_helper = & $this->helper('catnav');
			$catnav_helper->fromForum($topic['forum_id']);

			$subject = Agora::getPostVar('subject');
			$desc = Agora::getPostVar('desc');
			$icon = Agora::getPostVar('topic_icon');

			if ($subject) $topic['subject'] = $subject;
			if ($desc) $topic['descrip_t'] = $desc;
			if ($icon) $topic['icon_topic'] = $icon;
			
			$topic['subject'] = AString::escape($topic['subject']);
			$topic['descrip_t'] = AString::escape($topic['descrip_t']);
			
			$this->view->assign('topic',$topic);

			$post_model = & Model::getInstance('PostModel');

			$post_id = (int)Agora::getRequestVar('post_id');
			$post = $post_model->load($post_id);
			
			// member cannot edit other users topics
			if (!$this->agora_user['is_superadmin'] && !$can_edit && $post['poster_id'] != $this->agora_user['id']) {
				Agora::showMessage(Agora::lang('Access denied'));
				Agora::redirect(Agora::getRefferer());
			}

			$message = Agora::getPostVar('req_message', '');
			if ($message) $post['message'] = $message;
			
			$this->view->assign('post_id',$post_id);
			$this->view->assign('post_body',AString::escape($post['message']));
			$this->view->assign('topic_username', AString::escape((string) @$post['topic_username']));
			$this->view->assign('guest_email', AString::escape((string) @$post['guest_email']));
		}

		function preview()
		{
			$topic_icon = intval(Agora::getPostVar('topic_icon',0));
			$desc = Agora::getPostVar('desc','');
			$subject = Agora::getPostVar('subject','');

			$raw_message = Agora::getPostVar('req_message','');
			$message = $raw_message;
			$hide_smilies = Agora::getPostVar('hide_smilies',0);

			$post_id = (int)Agora::getRequestVar('post_id');
			$topic_id = (int)Agora::getRequestVar('id');

			$parser = $this->helper('parser');

			$message = $parser->parseMessage($raw_message, $hide_smilies);

			$signature = '';
			if ($this->agora_user['show_sig']) {
				$signature = $parser->parseSignature($this->agora_user['signature']);
			}

			$post_model = & Model::getInstance('PostModel');
			$user_model = & Model::getInstance('UserModel');
			$topic_model = & Model::getInstance('TopicModel');
			
			$post = $post_model->load($post_id);
			$post['user'] = $user_model->load($post['poster_id']);

			$user_helper = &$this->helper('user');

			$post['user'] = $user_helper->prepareUserAvatar($post['user']);
			$post['user'] = $user_helper->prepareUserTitle($post['user']);
			
//			$post['id'] = 0;
/*			$post['poster_id'] = $this->agora_user['id'];
			$post['poster'] = $this->agora_user['username'];
			$post['posted'] = time();*/

			$post['message'] = $message;
			$post['user']['signature'] = $signature;

			$topic = $topic_model->load($topic_id);
			$topic['status'] = 'new';
			// we don't know id now
			$topic['icon_topic'] = $topic_icon;
			$topic['subject'] = $subject;
			$topic['descrip_t'] = $desc;

			$this->view->assignRef('topic',$topic);

			$this->view->assignRef('post',$post);
			$this->view->assign('access',array());
			$this->view->assign('post_num','-');
			$this->view->assign('topic_id',$topic_id);
			$this->view->assign('post_id',$post_id);

			$this->view->assign('topic_icon',$topic_icon);
			$this->view->assign('raw_message',$raw_message);
			$this->view->assign('hide_smilies',$hide_smilies);

/*			$this->view->assign('merge',$merge);
			$this->view->assign('subscribe',$subscribe);*/

			$this->view->template = 'post/preview_topic';

			$this->display();
			jexit(0);
		}
		
		function _save() {
			if (Agora::getPostVar('preview')) {
				$this->_preview();
				return;
			}
			$can_edit = $this->access_model->authenticateTopic($this->agora_user['id'], $this->id,'edit_topic');
			
			$topic_model = & Model::getInstance('TopicModel');

			$subject = Agora::getPostVar('subject','No subject');
			$desc = Agora::getPostVar('desc','');

			$message = Agora::getPostVar('req_message','', true);
			$parser = & $this->helper('parser');
			$message = $parser->fix_broken_bbcode($message);
//			$subscribe = Agora::getPostVar('subscribe',0);
			$hide_smilies = Agora::getPostVar('hide_smilies',0);
			$silent = Agora::getPostVar('silent',0);
			
			$topic_icon = intval(Agora::getPostVar('topic_icon',0));

			$post_id = (int)Agora::getRequestVar('post_id');
			$post_model = & Model::getInstance('PostModel');			
			$post = $post_model->load($post_id);
			
			// member cannot edit other users posts posts
			if (!$this->agora_user['is_superadmin'] && !$can_edit && $post['poster_id'] != $this->agora_user['id']) {
				Agora::showMessage(Agora::lang('Access denied'));
				Agora::redirect(Agora::getRefferer());
			}
			
			$topic_model->edit($this->id, $subject,$desc, $topic_icon, $message, $hide_smilies);
			
			$post_model->edit($post_id,$message,$silent, $this->agora_user['username'], $post['poster']);

            if ( strpos($_SERVER['PHP_SELF'],'index2.php')!=0 ) {
				$mode = 'agroup';
            } else { 
				$mode = '';
			}
            $url = array('task'	=> 'topic', 'id' => $this->id);
            
            if ('agroup' == $mode) {
				Agora::redirect(Agora::makeURL($url,true,true));
            } else {
				$this->redirect('task=topic','id='.$this->id,'!page');
			}
		}

		function saved() {
			$this->_default();
		}

	}
